#!/bin/sh
#error and exit = set -e
#set -o errexit
#color
source /etc/init.d/functions
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

function createConfig(){
mkdir -p /etc/docker/
cat>/etc/docker/daemon.json<<\EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": [
    "https://2ph0ua9a.mirror.aliyuncs.com",
    "https://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn"
  ],
  "data-root":"/var/lib/docker",
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "50m",
    "max-file": "6"
  },
  "userland-proxy": true,
  "_comment": {"bip": "172.17.0.0/16"},
  "live-restore": true,
  "ipv6": false
}
EOF
}

function deal(){
  if [ ! -f /etc/docker/daemon.json ] ;then
     createConfig
  fi
  if [ ! -f /etc/yum.repos.d/docker-ce.repo ] ;then
     curl -sL http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
  fi
  export docker_version=20.10.23
  version=$(yum list docker-ce.x86_64 --showduplicates|sort -r|grep ${docker_version}|awk '{print $2}')
  yum -y install --setopt=obsoletes=0 docker-ce-${docker_version} docker-ce-rootless-extras-${docker_version} docker-ce-cli-${docker_version} docker-compose-plugin > /dev/null 2>&1
  # sed -i '/^LimitNOFILE=infinity/s@infinity@1048576@g' /usr/lib/systemd/system/docker.service
  # sed -i '/^LimitNOFILE=infinity/s@infinity@1048576@g' /usr/lib/systemd/system/containerd.service
  # systemctl daemon-reload
  containerd config default > /etc/containerd/config.toml
  sed -i 's@registry.k8s.io@registry.aliyuncs.com/google_containers@g' /etc/containerd/config.toml
  sed -i s@'SystemdCgroup = false'@'SystemdCgroup = true'@g /etc/containerd/config.toml
  sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors/a\        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]' /etc/containerd/config.toml
  sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"/a\        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"]' /etc/containerd/config.toml
  sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"/a\        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."ghcr.io"]' /etc/containerd/config.toml
  sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."ghcr.io"/a\          endpoint = ["https://registry-1.docker.io", "https://ghcr.io"]' /etc/containerd/config.toml
  sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"/a\          endpoint = ["https://gcr.azk8s.cn", "https://gcr.io"]' /etc/containerd/config.toml
  sed -i '/plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"/a\          endpoint = ["https://fogjl973.mirror.aliyuncs.com", "https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn","https://registry-1.docker.io"]' /etc/containerd/config.toml
  systemctl enable --now containerd > /dev/null 2>&1
}


function main(){
  action "$0" deal
  # yum -y install yum-plugin-versionlock
  # yum versionlock docker-ce
  # yum versionlock delete docker-ce
  # yum versionlock clear
}

main $*
